/*
 * Copyright (c) 2009 by Thomas Busey and Ruj Akavipat
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the Experteyes nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Thomas Busey and Ruj Akavipat ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Thomas Busey and Ruj Akavipat BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package buseylab.fiteyemodel.gui;

import java.awt.Rectangle;
import java.awt.image.BufferedImage;

/*
 * SearchSpacePanel.java
 *
 * Created on March 20, 2008, 3:48 PM
 */
/**
 * This creates a panel for search space controls, such as width/height
 * and tools for determining the appropriate values such as max/min/avereaged images
 * 
 * @author  dwyatte
 */
public class SearchSpacePanel extends javax.swing.JPanel {

    FitEyeModelSystemInterface parent;
    BufferedImage oldImg = null;
    private boolean isComputeMinMaxAverage = false;

    /** Creates new form SearchSpacePanel */
    public SearchSpacePanel() {
        initComponents();
        // default should be the initially set button        
        setDefaultButton();
    }

    public void setParent(FitEyeModelSystemInterface parent) {
        this.parent = parent;
    }

    // call this to reset the button group to default and therefore set the
    // image to normal
    public void setDefaultButton() {
        defaultButton.setSelected(true);
        oldImg = null;
    }

    void setArea(int width, int height) {
        this.widthSlider.setValue(width);
        this.heightSlider.setValue(height);
    }

    public void enableComputeMinMaxAvg() {
        this.isComputeMinMaxAverage = false;
        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("buseylab/fiteyemodel/resources/MinMaxAvgProcessor");
        computeMinMaxAvgButton.setText(bundle.getString("Compute Min, Max, Avg Button Text"));
    }

    public void setMaximumHeightWidth(int width, int height) {
        this.widthSlider.setMaximum(width);
        this.heightSlider.setMaximum(height);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        widthSlider = new javax.swing.JSlider();
        heightSlider = new javax.swing.JSlider();
        widthLabel = new javax.swing.JLabel();
        heightLabel = new javax.swing.JLabel();
        toolsLabel = new javax.swing.JLabel();
        defaultButton = new javax.swing.JRadioButton();
        minButton = new javax.swing.JRadioButton();
        maxButton = new javax.swing.JRadioButton();
        avgButton = new javax.swing.JRadioButton();
        computeMinMaxAvgButton = new javax.swing.JButton();

        widthSlider.setMaximum(512);
        widthSlider.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                widthSliderStateChanged(evt);
            }
        });

        heightSlider.setMaximum(512);
        heightSlider.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                heightSliderStateChanged(evt);
            }
        });

        widthLabel.setText("Width"); // NOI18N

        heightLabel.setText("Height"); // NOI18N

        toolsLabel.setText("Tools"); // NOI18N

        buttonGroup1.add(defaultButton);
        defaultButton.setText("Default"); // NOI18N
        defaultButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                defaultButtonActionPerformed(evt);
            }
        });

        buttonGroup1.add(minButton);
        minButton.setText("Min"); // NOI18N
        minButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                minButtonActionPerformed(evt);
            }
        });

        buttonGroup1.add(maxButton);
        maxButton.setText("Max"); // NOI18N
        maxButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                maxButtonActionPerformed(evt);
            }
        });

        buttonGroup1.add(avgButton);
        avgButton.setText("Average"); // NOI18N
        avgButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                avgButtonActionPerformed(evt);
            }
        });

        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("buseylab/fiteyemodel/resources/MinMaxAvgProcessor"); // NOI18N
        computeMinMaxAvgButton.setText(bundle.getString("Compute Min, Max, Avg Button Text")); // NOI18N
        computeMinMaxAvgButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                computeMinMaxAvgButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, heightLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, widthLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 49, Short.MAX_VALUE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(heightSlider, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 269, Short.MAX_VALUE)
                            .add(widthSlider, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 269, Short.MAX_VALUE)))
                    .add(layout.createSequentialGroup()
                        .add(toolsLabel)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(defaultButton)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(minButton)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(maxButton)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(avgButton))
                    .add(computeMinMaxAvgButton))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(widthLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(widthSlider, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(heightLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 34, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(heightSlider, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(toolsLabel)
                    .add(defaultButton)
                    .add(minButton)
                    .add(maxButton)
                    .add(avgButton))
                .add(18, 18, 18)
                .add(computeMinMaxAvgButton)
                .addContainerGap(37, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
    private void avgButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_avgButtonActionPerformed

        parent.setImage(parent.getImageProcessor().getAvgImg());

    }//GEN-LAST:event_avgButtonActionPerformed

    private void maxButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_maxButtonActionPerformed
        parent.setImage(parent.getImageProcessor().getMaxImg());
    }//GEN-LAST:event_maxButtonActionPerformed

    private void minButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_minButtonActionPerformed
        parent.setImage(parent.getImageProcessor().getMinImg());
    }//GEN-LAST:event_minButtonActionPerformed

    private void defaultButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_defaultButtonActionPerformed

        parent.setImage(null);

    }//GEN-LAST:event_defaultButtonActionPerformed

    private void widthSliderStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_widthSliderStateChanged
        // set the rect to it's old location with new width/height
        Rectangle oldSearchRect = parent.getInteractivePanel().getSearchRect();
        Rectangle newSearchRect = new Rectangle((int) oldSearchRect.getX(), (int) oldSearchRect.getY(), widthSlider.getValue(), heightSlider.getValue());
        parent.getInteractivePanel().setSearchRect(newSearchRect);
    }//GEN-LAST:event_widthSliderStateChanged

    private void heightSliderStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_heightSliderStateChanged
        // set the rect to it's old location with new width/height
        Rectangle oldSearchRect = parent.getInteractivePanel().getSearchRect();
        Rectangle newSearchRect = new Rectangle((int) oldSearchRect.getX(), (int) oldSearchRect.getY(), widthSlider.getValue(), heightSlider.getValue());
        parent.getInteractivePanel().setSearchRect(newSearchRect);
    }//GEN-LAST:event_heightSliderStateChanged

private void computeMinMaxAvgButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_computeMinMaxAvgButtonActionPerformed
    if (this.isComputeMinMaxAverage) {
        parent.stopMinMaxAverageImageComputation();
    } else {
        this.isComputeMinMaxAverage = true;
        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("buseylab/fiteyemodel/resources/MinMaxAvgProcessor");
        computeMinMaxAvgButton.setText(bundle.getString("Stop Compute Min Max Avg Button Lable"));
        parent.startMinMaxAverageImageComputation();
    }
}//GEN-LAST:event_computeMinMaxAvgButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JRadioButton avgButton;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton computeMinMaxAvgButton;
    private javax.swing.JRadioButton defaultButton;
    private javax.swing.JLabel heightLabel;
    private javax.swing.JSlider heightSlider;
    private javax.swing.JRadioButton maxButton;
    private javax.swing.JRadioButton minButton;
    private javax.swing.JLabel toolsLabel;
    private javax.swing.JLabel widthLabel;
    private javax.swing.JSlider widthSlider;
    // End of variables declaration//GEN-END:variables
}
